﻿using Knlght.CH.Domain.IRepository;
using Knlght.Domain;
using Knlght.Model.news;
using Knlght.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Knlght.Service
{
    public class NewsService : ServiceBase
    {
        #region NewsType
        public ActionResponse AddType(NewsType model)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.Add(model);
            });
        }
        public ActionResponse UpdateType(NewsType model)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.Update(model);
            });
        }
        public ActionResponse UpdateType(QueryRequest<NewsType> query, Expression<Func<NewsType, NewsType>> key)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.UpdateToList(query, key);
            });
        }      
        public ActionResponse RemoveType(QueryRequest<NewsType> query)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.Delete(query);
            });
        }
        public PagingResponse<NewsType> PagingType(PagingRequest<NewsType> request)
        {
            
            return GetPaging(() =>
            {
                //总记录
                int totalRecord;
                var rp1 = getRepository<INewsTypeRepository>();
                return new PagingList<NewsType>()
                {
                    Models = rp1.GetList(request.PageSize, request.PageIndex, out totalRecord, request.Query).ToList(),
                    TotalModels = totalRecord,
                    TotalPages = (totalRecord + request.PageSize - 1) / request.PageSize
                };
            });

        }
        public ListResponse<NewsType> GetNewsTypeList()
        {
            return GetList<NewsType>(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.GetList();
            });
        }
        public ListResponse<NewsType> GetNewsTypeList(QueryRequest<NewsType> query)
        {
            return GetList(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.GetList(query);
            });
        }
        public ListResponse<NewsType> GetNewsTypeListOnCache()
        {
            return GetList<NewsType>(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.GetListOnCache(null);
            });
        }
        public ItemResponse<NewsType> GetNewsType(QueryRequest<NewsType> query)
        {
            return GetModel<NewsType>(() =>
            {
                var rp = getRepository<INewsTypeRepository>();
                return rp.GetModel(query);
            });
        }
        #endregion

        #region NewsInfo
        public ActionResponse Add(NewsInfo model)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsInfoRepository>();
                return rp.Add(model);
            });
        }
        public ActionResponse Update(NewsInfo model)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsInfoRepository>();
                return rp.Update(model);
            });
        }
        public ActionResponse Update(QueryRequest<NewsInfo> query, Expression<Func<NewsInfo, NewsInfo>> key)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsInfoRepository>();
                return rp.UpdateToList(query, key);
            });
        }
        public ActionResponse Remove(QueryRequest<NewsInfo> query)
        {
            return doAction(() =>
            {
                var rp = getRepository<INewsInfoRepository>();
                return rp.Delete(query);
            });
        }
        public PagingResponse<NewsInfo> Paging(PagingRequest<NewsInfo> request)
        {
            return GetPaging<NewsInfo>(() => {
                //总记录
                int totalRecord =0 ;
                var rp = getRepository<INewsInfoRepository>();
               // var SSS = rp.GetListScalar(request.Query, o => new NewsInfo() { IsBlank = o.IsBlank });  
                return new PagingList<NewsInfo>()
                {
                    Models = rp.GetList(request.PageSize, request.PageIndex, out totalRecord, request.Query),
                    TotalModels = totalRecord,
                    TotalPages = (totalRecord + request.PageSize - 1) / request.PageSize
                };
            });
        }
        #endregion        
    }
}
